﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Reports.Core
{
    class ProtoGridColumnCollection : IReportLayoutColumnCollection
    {
        int index = -1;

        List<ReportLayoutColumn> list = new List<ReportLayoutColumn>();

        public ProtoGridColumnCollection(List<ReportLayoutColumn> list)
        {
            this.list = list;
        }

        public IReportLayoutColumn this[int index]
        {
            get
            {
                if (index < 0 || index > list.Count - 1)
                    return null;

                return list[index];
            }
        }

        public IReportLayoutColumn InsertColumn(int columnIndex)
        {
            IReportLayoutColumn formercolumn = this[columnIndex];

            if (formercolumn == null)
                return null;

            return InsertColumn(columnIndex, formercolumn.Width / 2);
        }

        public IReportLayoutColumn InsertColumn(int columnIndex, int width)
        {
            IReportLayoutColumn formercolumn = this[columnIndex];

            if (formercolumn == null)
                return null;

            if (width > formercolumn.Width || width < 0)
                return null;

            ReportLayoutColumn column = new ReportLayoutColumn(width);

            formercolumn.SetWidth(formercolumn.Width - width);

            list.Insert(columnIndex, column);

            ResetColumnIndex();

            return column;
        }

        public IReportLayoutColumn AddColumn(int width)
        {
            ReportLayoutColumn column = new ReportLayoutColumn(width);

            list.Add(column);

            ResetColumnIndex();

            return column;

        }

        public void ResetWidth(int maxWidth)
        {
            int orgWidth = 0;

            foreach (IReportLayoutColumn column in list)
            {
                orgWidth += column.Width;
            }

            foreach (IReportLayoutColumn column in list)
            {
                double newWidth = (double)column.Width * (double)maxWidth / (double)orgWidth;

                column.SetWidth((int)newWidth);
            }
        }

        public IReportLayoutColumn RemoveAt(int index)
        {
            ReportLayoutColumn rcolumn = this[index] as ReportLayoutColumn;

            if (rcolumn == null)
                return null;

            int colindex = rcolumn.ColumnIndex;

            int colwidth = rcolumn.Width;

            bool islastcol = (colindex == list.Count - 1);

            //不删除最后一列

            if (islastcol)
                return null;

            list.Remove(rcolumn);

            ResetColumnIndex();

            list[colindex].SetWidth(list[colindex].Width + colwidth);

            return rcolumn;
        }

        public int Count
        {
            get { return list.Count; }
        }

        private void ResetColumnIndex()
        {
            int counter = 0;

            foreach (ReportLayoutColumn column in list)
            {
                column.ColumnIndex = counter++;
            }
        }

        #region IEnumerator<IReportLayoutColumn> member

        public IReportLayoutColumn Current
        {
            get
            {
                return list[index];
            }
        }

        #endregion

        #region IDisposable member

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator member

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        public bool MoveNext()
        {
            if (index >= list.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion

        #region IEnumerable<IReportLayoutColumn> member

        public IEnumerator<IReportLayoutColumn> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

    }
}
